<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category   Zeed
 * @package    Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright  Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author     Zeed Team (http://blog.zeed.com.cn)
 * @since      2011-4-28
 * @version    SVN: $Id$
 */
class Attachment_Model_Folder extends Zeed_Db_Model_Mongo
{
    /**
     * @var string The table name.
     */
    protected $_name = 'folder';
    
    /**
     * @var integer Primary key.
     */
    protected $_primary = '_id';
    
    /**
     * @var string Table prefix.
     */
    protected $_prefix = 'attachment_';
    
    /**
     * 获取用户指定目录下的所有目录列表 - 支持关键字搜索
     * 
     * @param integer $userid 用户 ID
     * @param integer $parent_id 指定目录 ID
     * @param string $key 搜索关键字
     * @return array
     */
    public function getAllFoldersByParentid($userid, $parent_id = 0, $key = null)
    {
        $cols = array('_id', 'fid', 'parent_id', 'hid', 'title', 'is_has_children', 'mtime');
        $select = $this->where('userid', $userid)->where('parent_id', (int) $parent_id)->where('status', 1);
        if ($key) {
            $select->where('title', 'like', '%' . $key . '%');
        }
        $result = $select->orderBy('mtime', 'DESC')->get($cols);
        if (! empty($result)) {
            return $result->toArray();
        }
        return null;
    }
    
    /**
     * 计算下一个用户目录序号 ID
     * 
     * @param integer $userid 用户 ID
     * @return integer
     */
    public function nextFid($userid)
    {
        $latest = $this->fetchByWhere(array('userid' => $userid), array('fid' => 'DESC'), 1);
        if (! empty($latest)) {
            $fid = $latest[0]['fid'] + 1;
        } else {
            $fid = 1;
        }
        return $fid;
    }
    
    /**
     * 根据 fid 获取指定用户的目录信息
     *
     * @param integer $userid 用户 ID
     * @param string $fid 指定目录的序号 ID，多个以半角逗号分隔
     * @param array $cols 要查询的字段
     * @return array
     */
    public function getFolderByFid($userid, $fid = '', $cols = array())
    {
        $where = array('userid' => (int) $userid);
        
        /* 处理 fid */
        if (! empty($fid) && (is_string($fid) || is_integer($fid))) {
            $fid = explode(',', $fid);
        }
        if (! empty($fid) && is_array($fid)) {
            foreach ($fid as $k => $v) {
                $fid[$k] = (int) $v;
            }
            $where['fid'] = array('$in' => $fid);
        }
        
        $result = $this->fetchByWhere($where, null, null, null, $cols);
        return ! empty($result) ? $result : null;
    }
    
    /**
     * 获取指定根目录、指定行动下的所有子目录 - 下拉选项型
     * 
     * @param string $hid 根目录的 hid
     * @param string $card_id 行动 ID
     * @param integer $userid 用户 ID
     */
    public function getAllFoldersForSelect($hid, $card_id = null, $userid = null)
    {
        $where = array(
                'status' => 1,
                '$or' => array(
                        array('hid' => new MongoRegex("/$hid/i")),
                )
        );
        if ($card_id) {
            $where['card_id'] = $card_id;
        }
        if ($userid) {
            $where['userid'] = $userid;
        }
        $order = array('hid' => 'ASC');
        $folders = $this->fetchByWhere($where, $order);
        if (! empty($folders)) {
            $nv = array();
            foreach ($folders as $k => &$v) {
                /* 处理根目录 */
                if ($v['parent_id'] == 0) {
                    $v['title'] = '根目录';
                }
                
                /* 处理缩进 */
                $str_padding = '';
                $level = count(explode(':', $v['hid'])) - 2;
                if ($level) {
                    for ($i = 0; $i < $level; $i++) {
                        $str_padding .= "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                }
                $v['str_padding'] = $str_padding;
            }
        }
        return $folders;
    }
    
    /**
     * @return Attachment_Model_Folder
     */
    public static function instance ()
    {
        return parent::_instance(__CLASS__);
    }
}
